package com.cz.ssyx.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.cz.ssyx.common.result.Result;
import com.cz.ssyx.model.product.Attr;
import com.cz.ssyx.product.service.AttrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 商品属性 前端控制器
 * </p>
 *
 * @author Joker
 * @since 2024-04-18
 */
@Api(tags = "属性管理")
//@CrossOrigin
@RestController
@RequestMapping("/admin/product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    @ApiOperation("根据属性组id查询属性列表")
    @GetMapping("/{groupId}")
    public Result getList(@PathVariable Long groupId) {
        LambdaQueryWrapper<Attr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attr::getAttrGroupId, groupId);
        List<Attr> list = attrService.list(queryWrapper);
        return Result.ok(list);
    }

    @ApiOperation("根据属性id查属性")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Long id) {
        Attr attr = attrService.getById(id);
        return Result.ok(attr);
    }

    @ApiOperation("新增属性")
    @PostMapping("/save")
    public Result save(@RequestBody Attr attr){
        attrService.save(attr);
        return Result.ok(null);
    }

    @ApiOperation("修改")
    @PutMapping("/update")
    public Result updateById(@RequestBody Attr attr) {
        attrService.updateById(attr);
        return Result.ok(null);
    }

    @ApiOperation("删除")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        attrService.removeById(id);
        return Result.ok(null);
    }

    @ApiOperation("根据id列表删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        attrService.removeByIds(idList);
        return Result.ok(null);
    }
}

